package com.l024.easyandroidlib.utils;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @Author: Cat
 * @CreateTime: 2022-07-29  15:22
 * @Description: 文件工具类
 * @Version: 1.0
 */
public class FileUtil {

    //创建目录
    public static boolean createDir(String dir){
        if(TextUtils.isEmpty(dir)){
            return false;
        }
        File f = new File(dir);
        if(f.exists()){
            return true;
        }else {
            return f.mkdirs();
        }
    }

    //创建文件
    public static boolean createFile(String file){
        if(TextUtils.isEmpty(file)){
            return false;
        }
        File f = new File(file);
        if(f.exists()){
            return true;
        }else {
            try {
                return f.createNewFile();
            } catch (IOException e) {
                return false;
            }
        }
    }

    //获取手机sdcard目录
    public static String getSdcardPath(){
        return Environment.getExternalStorageDirectory().getAbsolutePath();
    }

    //获取App的缓存目录 /mnt/sdcard/Android/data/包名/cache
    public static String getAppCachePath(Context context){
        return context.getExternalCacheDir().getAbsolutePath();
    }

    //获取App的文件目录 /mnt/sdcard/Android/data/包名/files/dirName为null就是 /mnt/sdcard/Android/data/包名/files目录
    public static String getAppFilesPath(Context context,String dirName){
        return context.getExternalFilesDir(dirName).getAbsolutePath();
    }

    // /data/data/包名/files 内部存储
    public static String getFilesDir(Context context){
        return context.getFilesDir().getAbsolutePath();
    }

    /**
     * 保存字符串到文件
     * @param savePath 保存路径
     * @param content 保存内容
     * @return
     */
    public static boolean saveStringToFile(String savePath,String content,String fileName,boolean isAppend){
        if(TextUtils.isEmpty(savePath)||TextUtils.isEmpty(content)){
            return false;
        }
        boolean mkdirs = createDir(savePath);
        Log.e("创建目录","创建目录:"+mkdirs);
        //写入文件
        File f = new File(savePath+File.separator+fileName);
        if(!f.exists()){
            boolean  mkdir = createFile(f.getAbsolutePath());
            Log.e("创建文件","创建文件:"+mkdir);
        }
        try {
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(f.getAbsolutePath(),isAppend));
            bufferedWriter.write(content);
            bufferedWriter.flush();
            bufferedWriter.close();
            return true;
        } catch (Exception e) {
            Log.e("写入文件异常",e.getMessage());
        }
        return false;
    }

    public static String readFile(String filePath){
        if(TextUtils.isEmpty(filePath)){
            return "";
        }
        File f = new File(filePath);
        if(!f.exists()){
            return "";
        }
        try {
            BufferedReader bufferedReader  = new BufferedReader(new FileReader(f.getAbsolutePath()));
            StringBuffer sb = new StringBuffer();
            String line = "";
            while ((line=bufferedReader.readLine())!=null){
                sb.append(line);
            }
            bufferedReader.close();
            return sb.toString();
        } catch (Exception e) {
            Log.e("读取文件异常",e.getMessage());
        }
        return "";
    }

}
